package com.winit.openapi.e2e.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.storagecostrule.DoiCalcService;
import com.winit.erp.spi.storagecostrule.command.DoiCalcCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * DOI费用相关接口
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * cly 	1.0  		2017年4月18日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/e2e/doifee")
public class E2EDoiFeeController extends BaseController {

    @Resource
    private DoiCalcService doiCalcService;

    /**
     * DOI费用计算
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/calculateFee")
    public String doiCalc(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        BigDecimal doiAmount = BigDecimal.ZERO;
        DoiCalcCommand command = new DoiCalcCommand();
        command.setCtx(CommandContext.getContext());
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;

            ValidateUtil.validNotNull(json, "dispWhCode", "DispatchWarehouse");
            ValidateUtil.validNotNull(json, "doiNum", "DoiNum");
            ValidateUtil.validNotNull(json, "length", "Length");
            ValidateUtil.validNotNull(json, "width", "Width");
            ValidateUtil.validNotNull(json, "height", "Height");            

            command.setDoiNum(json.getInteger("doiNum"));
            command.setDispWhCode(json.getString("dispWhCode"));
            command.setLength(json.getBigDecimal("length"));
            command.setWidth(json.getBigDecimal("width"));
            command.setHeight(json.getBigDecimal("height"));

            doiAmount = doiCalcService.doiCalc (command);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        responseMsg.setData(doiAmount);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

}
